/*
 *  Copyright 2019-2025 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.gas.service.impl;

import me.zhengjie.gas.domain.Oil;
import me.zhengjie.gas.service.OilService2;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.gas.repository.OilRepository;
import me.zhengjie.gas.service.dto.OilDto;
import me.zhengjie.gas.service.dto.OilQueryCriteria;
import me.zhengjie.gas.service.mapstruct.OilMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
 * @website https://eladmin.vip
 * @description 服务实现
 * @author Joe Tan
 * @date 2025-08-25
 **/
@Service
@RequiredArgsConstructor
public class OilServiceImpl implements OilService2 {

    private final OilRepository oilRepository;
    private final OilMapper oilMapper;

    @Override
    public PageResult<OilDto> queryAll(OilQueryCriteria criteria, Pageable pageable){
        Page<Oil> page = oilRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(oilMapper::toDto));
    }

    @Override
    public List<OilDto> queryAll(OilQueryCriteria criteria){
        return oilMapper.toDto(oilRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public OilDto findById(Long id) {
        Oil oil = oilRepository.findById(id).orElseGet(Oil::new);
        ValidationUtil.isNull(oil.getId(),"Oil","id",id);
        return oilMapper.toDto(oil);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Oil resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId(snowflake.nextId());
        oilRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Oil resources) {
        Oil oil = oilRepository.findById(resources.getId()).orElseGet(Oil::new);
        ValidationUtil.isNull( oil.getId(),"Oil","id",resources.getId());
        oil.copy(resources);
        oilRepository.save(oil);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            oilRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<OilDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (OilDto oil : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("dept_id", oil.getDeptId());
            map.put("oil_id", oil.getOilId());
            map.put("oil_name", oil.getOilName());
            map.put("oil_density", oil.getOilDensity());
            map.put("price", oil.getPrice());
            map.put("color", oil.getColor());
            map.put("created_ts", oil.getCreatedTs());
            map.put("last_updated_ts", oil.getLastUpdatedTs());
            map.put("remark", oil.getRemark());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}